/*
 * Copyright (C) 2011 bus.fm
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.busfm.util;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import android.util.Log;

/**
 * Title:LogUtil Description: LogUtil Copyright (c) 2011 www.bus.fm Inc. All
 * rights reserved. Company: bus.fm
 * 
 * @author jingguo0@gmail.com
 * 
 */
public class LogUtil {
	/*
	 * the switch of the log
	 * 
	 * @return boolean
	 */
	private static boolean checkLogStatus() {
		return Constants.DEBUG;
	}

	public static void e(String tag, String info) {
		if (checkLogStatus()) {
			StackTraceElement[] stack = (new Throwable()).getStackTrace();
			StackTraceElement ele = null;
			String className = "";

			if (stack.length > 1) {
				ele = stack[1];
				try {
					className = Class.forName(ele.getClassName())
							.getSimpleName();
				} catch (ClassNotFoundException e) {
				}
			}

			if (getDecideResult(ele)) {
				Log.e(tag, className + "::" + info);
			}
		}
	}

	public static void i(String tag, String info) {
		if (checkLogStatus()) {
			StackTraceElement[] stack = (new Throwable()).getStackTrace();
			StackTraceElement ele = null;
			String className = "";

			if (stack.length > 1) {
				ele = stack[1];
				try {
					className = Class.forName(ele.getClassName())
							.getSimpleName();
				} catch (ClassNotFoundException e) {
				}
			}

			if (getDecideResult(ele)) {
				Log.i(tag, className + "::" + info);
			}
		}
	}

	public static void i(String tag, int info) {
		if (checkLogStatus()) {
			StackTraceElement[] stack = (new Throwable()).getStackTrace();
			StackTraceElement ele = null;
			String className = "";

			if (stack.length > 1) {
				ele = stack[1];
				try {
					className = Class.forName(ele.getClassName())
							.getSimpleName();
				} catch (ClassNotFoundException e) {
				}
			}

			if (getDecideResult(ele)) {
				Log.i(tag, className + "::" + String.valueOf(info));
			}
		}
	}

	public static void i(String tag, double info) {
		if (checkLogStatus()) {
			StackTraceElement[] stack = (new Throwable()).getStackTrace();
			StackTraceElement ele = null;
			String className = "";

			if (stack.length > 1) {
				ele = stack[1];
				try {
					className = Class.forName(ele.getClassName())
							.getSimpleName();
				} catch (ClassNotFoundException e) {
				}
			}

			if (getDecideResult(ele)) {
				Log.i(tag, className + "::" + String.valueOf(info));
			}
		}
	}

	public static void i(String tag, long info) {
		if (checkLogStatus()) {
			StackTraceElement[] stack = (new Throwable()).getStackTrace();
			StackTraceElement ele = null;
			String className = "";

			if (stack.length > 1) {
				ele = stack[1];
				try {
					className = Class.forName(ele.getClassName())
							.getSimpleName();
				} catch (ClassNotFoundException e) {
				}
			}

			if (getDecideResult(ele)) {
				Log.i(tag, className + "::" + String.valueOf(info));
			}
		}
	}

	public static void i(String tag, float info) {
		if (checkLogStatus()) {
			StackTraceElement[] stack = (new Throwable()).getStackTrace();
			StackTraceElement ele = null;
			String className = "";

			if (stack.length > 1) {
				ele = stack[1];
				try {
					className = Class.forName(ele.getClassName())
							.getSimpleName();
				} catch (ClassNotFoundException e) {
				}
			}

			if (getDecideResult(ele)) {
				Log.i(tag, className + "::" + String.valueOf(info));
			}
		}
	}

	@SuppressWarnings("rawtypes")
	public static void i(String tag, String startMsg, Collection info) {
		if (checkLogStatus()) {
			StackTraceElement[] stack = (new Throwable()).getStackTrace();
			StackTraceElement ele = null;
			String className = "";

			if (stack.length > 1) {
				ele = stack[1];
				try {
					className = Class.forName(ele.getClassName())
							.getSimpleName();
				} catch (ClassNotFoundException e) {
				}
			}

			StringBuffer msg = null;

			if (startMsg == null) {
				msg = new StringBuffer(className + "::");
			} else {
				msg = new StringBuffer(className + "::" + startMsg);
			}

			Iterator itr = info.iterator();

			while (itr.hasNext()) {
				msg.append(itr.next().toString());
				msg.append(";");
			}

			if (getDecideResult(ele)) {
				Log.i(tag, msg.toString());
			}
		}
	}

	@SuppressWarnings("rawtypes")
	public static void i(String tag, String startMsg, HashMap info) {
		if (checkLogStatus()) {
			StackTraceElement[] stack = (new Throwable()).getStackTrace();
			StackTraceElement ele = null;
			String className = "";

			if (stack.length > 1) {
				ele = stack[1];

				try {
					className = Class.forName(ele.getClassName())
							.getSimpleName();
				} catch (ClassNotFoundException e) {
				}
			}

			StringBuffer msg = null;

			if (startMsg == null) {
				msg = new StringBuffer(className + "::");
			} else {
				msg = new StringBuffer(className + "::" + startMsg);
			}

			Set keySet = info.keySet();
			Iterator itr = keySet.iterator();

			while (itr.hasNext()) {
				Object key = itr.next();
				Object value = info.get(key);
				msg.append(key.toString());
				msg.append("=");
				msg.append(value.toString());
				msg.append(";");
			}

			if (getDecideResult(ele)) {
				Log.i(tag, msg.toString());
			}
		}
	}

	public static boolean getDecideResult(StackTraceElement ele) {
		return true;
	}
}
